package com.wzy.oa_sys.service.impl.attendance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.oa_sys.mapper.attendance.TimeStandardMapper;
import com.wzy.oa_sys.model.pojo.attendance.OaTimeStandards;
import com.wzy.oa_sys.model.request.attendance.StandardReq;
import com.wzy.oa_sys.service.attendance.TimeStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.List;

@Service
public class TimeStandardServiceImpl extends ServiceImpl<TimeStandardMapper, OaTimeStandards> implements TimeStandardService {

    @Autowired
    private TimeStandardMapper timeStandardMapper;

    @Override
    public void addAttendanceStandard(StandardReq standardReq) {
        OaTimeStandards oaTimeStandards = new OaTimeStandards();
        QueryWrapper<OaTimeStandards> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_type",standardReq.getType());

        oaTimeStandards.setCType(standardReq.getType());
        ZonedDateTime standardTime = standardReq.getStandardTime();
        oaTimeStandards.setDtTime(Time.valueOf(String.format("%02d:%02d:%02d",standardTime.getHour()+8,standardTime.getMinute(),standardTime.getSecond())));
        if(timeStandardMapper.selectOne(queryWrapper)!=null){
            oaTimeStandards.setNTimeId(timeStandardMapper.selectOne(queryWrapper).getNTimeId());
            timeStandardMapper.updateTimeStandardById(oaTimeStandards);
        }else{
            timeStandardMapper.insert(oaTimeStandards);
        }

    }

    @Override
    public List<OaTimeStandards> getStandards() {
        List<OaTimeStandards> oaTimeStandards = timeStandardMapper.selectList(null);
        return oaTimeStandards;
    }


}
